// /mock/api/LicenseApplication.js
const Mock = require('mockjs')
const Qs = require('qs')
const Random = Mock.Random
let LicenseApplication = [] // 用于存放通行证数据的数组
Random.extend({
  constellation: function () {
    const constellations = [
      '个人通行证',
      '企业通行证'
    ]
    return this.pick(constellations)
  },
  status: function () {
    const status = [
      '已拒绝',
      '申请中',
      '已审核',
      '已禁用'
    ]
    return this.pick(status)
  },
  licenseType: function () {
    const licenseType = [
      '渣土车通行证',
      '临时证'
    ]
    return this.pick(licenseType)
  },
  brandType: function () {
    const brandType = [
      '黄牌',
      '蓝牌'
    ]
    return this.pick(brandType)
  }
})
for (let i = 0; i < 185; i++) { // 生成100个对象放到数组中
  let template1 = {
    'id': i,
    'idNumber': Random.id(),
    'driverName': Random.cname(), // 生成姓名
    'startDate': Random.date(), // 生成一个随机日期,可加参数定义日期格式
    'endDate': Random.date(),
    'applyDate': Random.date(),
    'brandNumber': Random.string(4),
    'LicenseType': Random.constellation(),
    'licenseType': Random.licenseType(),
    'brandType': Random.brandType(),
    'applyRoute': Random.province() + '-' + Random.province(),
    'startPoint': Random.province(),
    'endPoint': Random.province(),
    'status': Random.status(),
    'company': '河北路和汇源食品贸易有限公司',
    'applyRecord': '',
    'srcList': [
      'http://passport.banging.cn/Fs5NQ1tfO-5gMEXjxCHGUZl2D5_R',
      'http://passport.banging.cn/Fr8ljP6jvKxYKwS6nIuWs1IQBtOM',
      'http://passport.banging.cn/FtjeECQO5GBG-mCYnDW9SYUz65Ld',
      'http://passport.banging.cn/FjbPVFAEK9xiCfsz942p96GBw_kA'
    ]
  }
  LicenseApplication.push(template1)
}
const getAllLicenseApplication = (params) => {
  if (params.body && LicenseApplication.length) {
    let pageInfo = JSON.parse(Qs.parse(params).body)
    let start = (pageInfo.current - 1) * pageInfo.size
    let end = pageInfo.current * pageInfo.size
    var total = LicenseApplication.length
    var applyList = LicenseApplication.slice(start, end)
  }
  return { applyList, total }
}
const getUnprocessedLicenseApplication = (params) => {
  var unprocessedLicenseApplication = LicenseApplication.filter(e => e.status === '申请中')
  if (params.body && unprocessedLicenseApplication.length) {
    let pageInfo = JSON.parse(Qs.parse(params).body)
    let start = (pageInfo.current - 1) * pageInfo.size
    let end = pageInfo.current * pageInfo.size
    var total = unprocessedLicenseApplication.length
    var applyList = unprocessedLicenseApplication.slice(start, end)
  }
  return { applyList, total }
}
const searchByBrandNumber = (params) => {
  if (params.body) {
    let obj = JSON.parse(Qs.parse(params).body)
    var res = LicenseApplication.filter(e => e.brandNumber === obj.brandNumber)
    var total = res.length
  }
  return { res, total }
}
const getLicenseInfo = (params) => {
  if (params.body) {
    let obj = JSON.parse(Qs.parse(params).body)
    console.log(obj)
    var res = LicenseApplication.filter(e => String(e.id) === String(obj.id))
  }
  return { res }
}
Mock.mock('/api/getAllLicenseApplication', 'get', getAllLicenseApplication) // 获取所有通行证申请列表
Mock.mock('/api/getUnprocessedLicenseApplication', 'get', getUnprocessedLicenseApplication) // 获取未处理通行证申请列表
Mock.mock('/api/searchByBrandNumber', 'get', searchByBrandNumber) // 通过号牌查询
Mock.mock('/api/getLicenseInfo', 'get', getLicenseInfo) // 获取通行证申请信息
